package com.shundr.auth.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shundr.auth.R;
import com.shundr.auth.base.BaseActivity;
import com.shundr.auth.common.model.VersionInfo;
import com.shundr.auth.common.view.DialodUpdateApp;
import com.shundr.auth.common.view.SharedPrefUtil;
import com.shundr.auth.frame.constant.LConstants;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.http.LHttpRequest;
import com.shundr.auth.frame.service.UpgradeService;
import com.shundr.auth.frame.util.GetVersionUtil;
import com.shundr.auth.frame.util.LLogger;
import com.shundr.auth.frame.util.StringUtil;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.dao.AreaDao;
import com.shundr.auth.user.http.UserRequest;
import com.shundr.auth.user.model.StaffInfo;

public class StaffLoginActivity extends BaseActivity {

	public static final String LAST_USER_NAME = "last_user_name";
	public static final String LAST_USER_PWD = "last_user_pwd";
	
	private EditText etName;
	private EditText etPw;

	private Button btnLogin;
	private Button btnRegister;

	private CheckBox checkBox;

	private SharedPrefUtil shPrefUtil;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.ADMIN_LOGIN_SUCCESSED:
				StaffInfo info = (StaffInfo) msg.obj;
				String token = info.accessToken;
				String staffQrCodePic = info.staffQrCodePic;

				// save staff qrcode pic url
				if (!StringUtil.isNull(staffQrCodePic)) {
					shPrefUtil.putString(LConstants.STAFF_QRCODE_PIC,
							staffQrCodePic);
					shPrefUtil.commit();
				}
				
				shPrefUtil.putString(LConstants.STAFF_PHONE, info.getStaffPhone());
				shPrefUtil.putInt(LConstants.STAFF_TYPE, info.staffTypte);
				shPrefUtil.commit();

				if (token != null) {
					LContext.setSTAFF_TOKEN(token);

					if (checkBox.isChecked()) {
						// set sharedPre: last login name&pwd
						shPrefUtil.putString(LAST_USER_NAME, etName.getText()
								.toString());
						shPrefUtil.putString(LAST_USER_PWD, etPw.getText()
								.toString());
						shPrefUtil.commit();
					}

					startActivity(new Intent(mContext, MainActivity.class));
					StaffLoginActivity.this.finish();
				}
				break;
			case UserRequest.GET_APPSYS_INFO_UPDATE:
				VersionInfo version = (VersionInfo) msg.obj;
				showInfoDialog(version);
				break;
			case LHttpRequest.REQUEST_FAILED:
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_login);

		shPrefUtil = new SharedPrefUtil(this, LConstants.PRE_NAME);

		etName = (EditText) findViewById(R.id.et_name);
		etPw = (EditText) findViewById(R.id.et_pw);

		checkBox = (CheckBox) findViewById(R.id.chk_remember);
		checkBox.setVisibility(View.VISIBLE);

		btnLogin = (Button) findViewById(R.id.btn_login);
		btnRegister = (Button) findViewById(R.id.btn_register);
		btnRegister.setVisibility(View.GONE);

		// get last login name&pwd
		String name = shPrefUtil.getString(LAST_USER_NAME, "");
		if ("".equals(name)) {
			checkBox.setChecked(false);
		} else {
			checkBox.setChecked(true);
			etName.setText(name);
			etPw.setText(shPrefUtil.getString(LAST_USER_PWD, ""));
		}

		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (check())
					new UserRequest(mContext, mHandler).staffLogin(etName
							.getText().toString(), etPw.getText().toString());
			}
		});

		btnRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// startActivity(new Intent(mContext, AuthListActivity.class));
			}
		});
		// new UserRequest(mContext, mHandler).staffLogin("admin", "admin");

		checkVersion();
	}

	private boolean check() {
		if (StringUtil.isNull(etName.getText().toString())) {
			ToastHelper.toastShort(mContext, "请输入用户名");
			return false;
		}
		if (StringUtil.isNull(etPw.getText().toString())) {
			ToastHelper.toastShort(mContext, "请输入密码");
			return false;
		}
		return true;
	}

	/**
	 * 检查版本更新
	 */
	private void checkVersion() {
		int VersionCode = GetVersionUtil
				.getVersionCode(getApplicationContext());
		int androidVersion = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
		new UserRequest(mContext, mHandler).getAppSysInfo(VersionCode + "",
				androidVersion + "");
	}

	private void showInfoDialog(VersionInfo version) {
		if (version == null)
			return;
		DialodUpdateApp dialodUpdateApp = new DialodUpdateApp(mContext, version);
		dialodUpdateApp.show();
	}

}
